// sgu342
// ml:run = $bin < input
// ml:ccf += -g
// ml:opt = 0
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>

namespace tue
{
	std::string remove_leading_zero(std::string const & s)
	{
		int i = 0;
		int l = s.size();
		while (i < l && s[i] == '0') i++;
		if (i == l) i--;
		return std::string(s.begin() + i, s.end());
	}

	struct big_integer;
	big_integer operator*(big_integer const & a, big_integer const & b);
	big_integer operator/(big_integer const & a, big_integer const & b);
	big_integer operator+(big_integer const & a, big_integer const & b);
	big_integer operator-(big_integer const & a, big_integer const & b);
	bool operator>(big_integer const & a, big_integer const & b);

	struct big_integer
	{
		big_integer(std::string const & num) : num(remove_leading_zero(num)) {}
		big_integer(int x) : num(std::to_string(x)) {}
		big_integer() {}

		std::string value() const { return num; }
        bool is_zero() { return remove_leading_zero(num)[0] == '0'; }

        // 123 => "123"
		std::string num;
	};

    std::istream & operator>>(std::istream & is, big_integer & a)
    {
        is >> a.num;
        return is;
    }

	std::ostream & operator<<(std::ostream & os, big_integer const & a)
	{
		os << a.value();
		return os;
	}

	big_integer operator*(big_integer const & a, big_integer const & b)
	{
		std::string const & s1 = a.value();
		std::string const & s2 = b.value();
		int l1 = s1.size();
		int l2 = s2.size();
		std::vector<int> s(2 * (l1 + l2));
		int l = 2 * (l1 + l2);
		for (int i = 0; i < l1; i++)
			for (int j = 0; j < l2; j++)
				s[l - i - j - 1] += (s1[l1 - i - 1] - '0') * (s2[l2 - j - 1] - '0');
		for (int i = 0; i < l; i++)
			if (s[l - i - 1] > 9) {
				int t = s[l - i - 1];
				s[l - i - 2] += t / 10;
				s[l - i - 1]  = t % 10;
			}
		std::string ts(2 * (l1 + l2), '0');
		for (int i = 0; i < 2 * (l1 + l2); i++)
			ts[i] += s[i];
		return big_integer(ts);
	}

	big_integer operator/(big_integer const & a, int x)
	{
		std::string const & s1 = a.value();
		int l1 = s1.size();
		std::vector<int> s(l1);
		int l = l1;
		for (int i = 0; i < l1; i++) s[i] = s1[i] - '0';
		for (int i = 0; i < l1; i++) {
			if (i < l1 - 1)
				s[i + 1] += (s[i] % x) * 10;
			s[i] /= x;
		}
		std::string ts(l, '0');
		for (int i = 0; i < l; i++)
			ts[i] += s[i];
		return big_integer(ts);
	}

	int operator%(big_integer & a, int x)
    {
		std::string const & s1 = a.value();
		int l1 = s1.size();
		std::vector<int> s(l1);
        int ret = 0;
		for (int i = 0; i < l1; i++) s[i] = s1[i] - '0';
		for (int i = 0; i < l1; i++) {
			if (i < l1 - 1) s[i + 1] += (s[i] % x) * 10;
            if (i == l1 - 1) ret = s[i] % x;
			s[i] /= x;
		}
		std::string ts(l1, '0');
		for (int i = 0; i < l1; i++)
			ts[i] += s[i];
        a = big_integer(ts);
		return ret;
    }

	big_integer operator+(big_integer const & a, big_integer const & b)
	{
		std::string const & s1 = (a > b) ? a.value() : b.value();
		std::string const & s2 = (a > b) ? b.value() : a.value();
		int l1 = s1.size() + 1;
		int l2 = s2.size();
		std::vector<int> s(l1);
		for (int i = 1; i < l1; i++) s[i] = s1[i - 1] - '0';
		for (int i = 0; i < l2; i++) {
			s[l1 - i - 1] += s2[l2 - i - 1] - '0';
			if (s[l1 - i - 1] > 9) {
				s[l1 - i - 1] -= 10;
				s[l1 - i - 2]++;
			}
		}
		std::string ret(l1, '0');
		for (int i = 0; i < l1; i++) ret[i] += s[i];
		return big_integer(ret);
	}

	big_integer operator-(big_integer const & a, big_integer const & b) // assume a > b
	{
		std::string const & s1 = a.value();
		std::string const & s2 = b.value();
		int l1 = s1.size();
		int l2 = s2.size();
		std::vector<int> s(l1);
		for (int i = 0; i < l1; i++) s[i] = s1[i] - '0';
		for (int i = 0; i < l2; i++) {
			s[l1 - i - 1] -= s2[l2 - i - 1] - '0';
			if (s[l1 - i - 1] < 0) {
				s[l1 - i - 1] += 10;
				s[l1 - i - 2]--;
			}
		}
		std::string ret(l1, '0');
		for (int i = 0; i < l1; i++) ret[i] += s[i];
		return big_integer(ret);
	}

	bool operator>(big_integer const & a, big_integer const & b)
	{
		std::string const & s1 = a.value();
		std::string const & s2 = b.value();
		int i = 0, j = 0;
		for (; s1[i] == '0'; i++);
		for (; s2[j] == '0'; j++);
		int l1 = s1.size(), l2 = s2.size();
		if (l1 - i != l2 - j) return l1 - i > l2 - j;
		for (int t = 0; t + i < l1 && t + j < l2; t++)
			if (s1[t + i] != s2[t + j])
				return s1[t + i] > s2[t + j];
		return false;
	}
}

long long b, n;

int main()
{
    std::ios_base::sync_with_stdio(false);
    tue::big_integer a;
    std::cin >> a;
    std::cin >> b;
    if (b == 1) { std::cout << a << "\n"; return 0; }
    long long ans = 0, rans = (1ll) << 44;
    for (; !a.is_zero(); ) {
        long long t = a % b;
        auto x = std::min(ans, rans) + t;
        auto y = std::min(ans + 1, rans - 1) + b - t;
        ans = x; rans = y;
    }
    std::cout << std::min(ans, rans) << "\n";
}

